<?php
/**
 * MergedFile Class
 *
 * @package Extensions
 * @version @@VERSION@@
 * @subpackage Low-Cal_Vanilla
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

if (!defined('IN_VANILLA')) exit();

require_once($Configuration['LIBRARY_PATH'] . 'Framework/Framework.Functions.php');
require_once('Functions.php');
require_once('Class.Cache.php');



/**
 * MergedFile Class.
 * @package Extensions
 * @subpackage Low-Cal_Vanilla
 */
class LowCalVanilla_MergedFile {
	var $_ArrayFiles;
	var $_Cache;
	var $_Content;
	var $_ContentEncoding;
	var $_Name;
	var $_Mime;
	var $_LastModification;
	var $_Size;
	var $Context;

	/**
	 * Constructor
	 * @return void
	 */
	function LowCalVanilla_MergedFile(&$Context, $ArrayFiles, $Mime = 'text/plain') {
		$this->Context = &$Context;
		$this->_Cache = new LowCalVanilla_Cache($this->Context);

		$this->SetName($ArrayFiles);
		$this->SetMime($Mime);
		$this->setContentEncoding();
	}

	/**
	 * Cache the merge file
	 * @return string
	 * @param string
	 */
	function CacheContent($Content)
	{
		$this->_Cache->Store($this->_Name, $Content);
		if (defined('FORCE_GZIP')) {
			$GZipContent = gzencode($Content);
			$this->_Cache->Store($this->_Name . 'gzip', $GZipContent);
		}

		if (LowCalVanilla_AcceptGzip($this->Context->Configuration)) {
			return $GZipContent;
		} else {
			return $Content;
		}
	}

	/**
	 * clear cache
	 * @return void
	 */
	function ClearCache() {
		$this->_Cache->ClearCache();
	}


	/**
	 * Return the merged content of the files
	 * @return string
	 */
	function GetContent(){
		$Content = '';
		if (!$Content = $this->GetCachedContent()) {
			$this->_Name = '';
			foreach ($this->_ArrayFiles as $File) {
				$Name = $File->GetPath();
				$FileContent = $File->GetContent();
				$LastModification = $File->GetLastModification();
				if ( $Name
					&& $FileContent
					&& $LastModification
				) {
					$Content .= $FileContent . "\n";
					$this->_Name .= $Name . $LastModification;
				} else {
					$this->Context->WarningCollector->Add('A File could not be merged.');
				}
			}
			if ($this->_Name) {
				$this->_Name .= $this->Context->Configuration['LOWCALVANILLA_VERSION'];
				$Content = $this->CacheContent($Content);
			}
		}
		$this->_Size = strlen($Content);
		return $Content;
	}

	/**
	 * Get the content from cache
	 * @return string
	 */
	function GetCachedContent(){
		$Content = '';
		if (defined('FORCE_GZIP')
			&& $this->_ContentEncoding
		) {
			$Content = $this->_Cache->Fletch($this->_Name . 'gzip');
		} else {
			$Content = $this->_Cache->Fletch($this->_Name);
		}
		return $Content;
	}

	/**
	 * Return the Content-Encoding of the merged files
	 */
	function GetContentEncoding() {
		return $this->_ContentEncoding;
	}


	/**
	 * Return details of the package formatted for Header
	 * @return array
	 */
	function GetDetails() {
		$Details = array();
		$Details['Expires'] = gmdate(
			'D, d M Y H:i:s',
			time() + ForceInt($this->Context->Configuration['LOWCALVANILLA_EXPIRE'], 3600)) . ' GMT';
		$Details['Last-Modified'] = gmdate('D, d M Y H:i:s', $this->GetLastModification()) . ' GMT';
		$Details['ETag'] = $this->GetETag();
		$Details['Content-Type'] = $this->GetMime();
		$Details['Content-Length'] = $this->GetSize();
		if ($this->_ContentEncoding) {
			$Details['Content-Encoding'] = $this->_ContentEncoding;
		}
		return $Details;
	}

	function GetETag() {
		return md5($this->_Name);
	}

	/**
	 * Return Mime
	 * @return string
	 */
	function GetMime() {
		return $this->_Mime;
	}

	/**
	 * Return the size of the merged filed
	 */
	function GetSize() {
		return $this->_Size;
	}


	/**
	 * Get last modification of the merge file - could be the oldest modification of the file array
	 * but we can't really rely on it. Instead we will use the cache one
	 */
	function GetLastModification() {
		return $this->_Cache->GetLastModification($this->_Name);
	}

	/**
	 * Check the client cached is up to date
	 * @return bool
	 * @param string $ETag
	 * @param int $MathTime
	 */
	function HasChanged($ETag, $MathTime) {
		if ($ETag) {
			// if Etag diferent, it has change
			if ($ETag != $this->GetETag()) {
				return true;
			}
		} else {
			// if no cache, it has changed
			if (!$LastModification = $this->GetLastModification()) {
				return true;
			}
			if ($MathTime < $LastModification) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Set content encoding
	 * @return string
	 */
	function setContentEncoding() {
		$this->_ContentEncoding = '';
		if (LowCalVanilla_AcceptGzip($this->Context->Configuration)){
			$this->_ContentEncoding = 'gzip';
		}
		return $this->_ContentEncoding;
	}


	/**
	 * Set Mine
	 * @return string
	 * @param string $Mine
	 */
	function SetMime($Mime) {
		$this->_Mime = $Mime;
		return $this->_Mime;
	}

	/**
	 * Set Name
	 * @return string
	 * @param array $ArrayFiles
	 */
	function SetName($ArrayFiles) {
		$this->_Name = '';
		$this->_ArrayFiles = array();
		foreach ($ArrayFiles as $File) {
			$Name = $File->GetPath();
			$LastModification = $File->GetLastModification();
			if ( $Name
				&& $LastModification
			) {
				$this->_Name .= $Name . $LastModification;
				$this->_ArrayFiles[] = $File;
			} else {
				$this->Context->WarningCollector('A file could not be found.');
			}
		}
		if ($this->_Name) {
			$this->_Name .= $this->Context->Configuration['LOWCALVANILLA_VERSION'];
		}
		return $this->_Name;
	}
}